﻿using ShopPowerTemplate.Common;
using ShopPowerTemplate.Entities;
using ShopPowerTemplate.WebServiceReference;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;

namespace ShopPowerTemplate.ViewModels
{
    public class SubcategoryProductListViewModel : BindableBase
    {
        const string AllCategories = "Todas las subcategorias";
        const string SortDefault = "Ordenar por";

        public enum SortMode
        {
            Alphabetical,
            Price,
            Rate,
            Default,
        };

        public class SortElement
        {
            public SortMode Mode { get; set; }
            public string Value { get; set; }
        }

        public Category CurrentCategory { get; set; }

        private string _title = "Productos";
        public string Title
        {
            get { return _title; }
            set { this.SetProperty(ref this._title, value); }
        }

        private ObservableCollection<ProductPresenterViewModel> _products = new ObservableCollection<ProductPresenterViewModel>();
        public ObservableCollection<ProductPresenterViewModel> Products
        {
            get { return _products; }
            set { this.SetProperty(ref this._products, value); }
        }

        private ObservableCollection<Subcategory> _subcategories = new ObservableCollection<Subcategory>();
        public ObservableCollection<Subcategory> Subcategories
        {
            get { return _subcategories; }
            set { this.SetProperty(ref this._subcategories, value); }
        }

        private Subcategory _selectedSubcategory;
        public Subcategory SelectedSubcategory
        {
            get { return _selectedSubcategory; }
            set 
            {
                if (this.SetProperty(ref this._selectedSubcategory, value))
                {
                    UpdateProducts(SelectedSubcategory, SelectedSort);
                }
            }
        }

        private ObservableCollection<SortElement> _sortItems;
        public ObservableCollection<SortElement> SortItems
        {
            get { return _sortItems; }
            set { this.SetProperty(ref this._sortItems, value); }
        }

        private SortElement _selectedSort;
        public SortElement SelectedSort
        {
            get { return _selectedSort; }
            set
            {
                if (this.SetProperty(ref this._selectedSort, value))
                {
                    UpdateProducts(SelectedSubcategory, SelectedSort);
                }
            }
        }

        private async void UpdateProducts(Subcategory sub, SortElement selectedSort)
        {
            Products = new ObservableCollection<ProductPresenterViewModel>();

            WebServiceClient service = new WebServiceClient();
            if (sub.Name == AllCategories) sub = null;

            ObservableCollection<Product> products = null;

            switch (selectedSort.Mode)
            {
                case SubcategoryProductListViewModel.SortMode.Alphabetical:
                    products = await service.SearchProductCustomByNameAsync(CurrentCategory, sub);
                    //products = new ObservableCollection<Product>(products.Reverse()); //Tito 07-12-12 El orden era inverso
                    break;
                case SubcategoryProductListViewModel.SortMode.Price:
                    products = await service.SearchProductCustomByPriceAsync(CurrentCategory, sub);
                    break;
                case SubcategoryProductListViewModel.SortMode.Rate:
                    products = await service.SearchProductCustomByRatingAsync(CurrentCategory, sub);
                    break;
                default:
                    products = await service.SearchProductCustomByProminentAndNameAsync(CurrentCategory, sub);
                    break;
            }

            Products = new ObservableCollection<ProductPresenterViewModel>(products.Select(p => new ProductPresenterViewModel(p)));
        }

        public SubcategoryProductListViewModel(Category c)
        {
            Title = c.IdentityDescription;
            CurrentCategory = c;
            LoadSortElements();
            LoadSubcategoryListFromCategory(c);
        }

        private void LoadSortElements()
        {
            _selectedSort = new SortElement { Mode = SortMode.Default, Value = SortDefault };
            _sortItems = new ObservableCollection<SortElement>();
            _sortItems.Add(new SortElement { Mode = SortMode.Price, Value = "Precio" });
            _sortItems.Add(new SortElement { Mode = SortMode.Alphabetical, Value = "Alfabético" });
            _sortItems.Add(new SortElement { Mode = SortMode.Rate, Value = "Rating" });
            _sortItems.Insert(0, _selectedSort);
        }

        protected async void LoadSubcategoryListFromCategory(Category c)
        {
            var selectedItem = new Subcategory { Name = AllCategories };

            WebServiceClient service = new WebServiceClient();
            Subcategories = await service.CategoryGetSubcategoriesForAsync(c);
            Subcategories.Insert(0, selectedItem);

            SelectedSubcategory = selectedItem;
        }

    }
}
